{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from service.BaseEngine import BaseEngine\n",
      "from utils.auto_load import AutoLoad\n",
      "import pandas as pd\n",
      "from influxdb.resultset import ResultSet\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from service.BaseEngine import BaseEngine\n",
      "from utils.auto_load import AutoLoad\n",
      "from service.DataSource import DataSource\n",
      "from perioddetection import autoperiod\n",
      "loader = AutoLoad()\n",
      "engine = loader.auto_load_engine_default(method='SHESD')\n",
      "# Cau hinh lai db name va measurement tro toi CSDL Influxdb\n",
      "# Service tu dong load 20 ngay gan nhat de du doan anomaly\n",
      "datasource = DataSource(db_name='example',measurement='vc1_2weeks')\n",
      "service = BaseEngine(engine=None,datasource=datasource, number_of_days=20)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Please plug in Engine instance of BaseOutlier\n",
        "Connect to database server"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = engine.convert_twitter_format(service.query_all().value)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Get latest time series points\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "autoperiod.period_detect(a,segment_method = \"topdownsegment\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "[4323, 2884]"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import os\n",
      "from datetime import datetime\n",
      "import time\n",
      "import json\n",
      "import numpy as np\n",
      "import random\n",
      "from influxdb import InfluxDBClient, DataFrameClient"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dat = pd.read_csv('data/dataset1/10.json_remake',index_col=0,parse_dates=True)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "client = DataFrameClient(database='test')\n",
      "new_dat = engine.convert_to_influx_format(dat.points).interpolate()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib\n",
      "new_dat.plot()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Using matplotlib backend: Qt4Agg\n"
       ]
      },
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 22,
       "text": [
        "<matplotlib.axes._subplots.AxesSubplot at 0x7f024b3fd9d0>"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "client.write_points(new_dat[:14*24*60],\"vc10\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "new_dat.shape[0] - new_dat[:14*24*60].shape[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 31,
       "text": [
        "974"
       ]
      }
     ],
     "prompt_number": 31
    }
   ],
   "metadata": {}
  }
 ]
}